236template <
class TypeBase,
typename... TypeInput>
244 typedef std::map<std::string, CreatorCallback> CallbackMap;
261 static TypeBase
create(
const std::string& _type, TypeInput... _input);
280 CallbackMap callbacks_;
309template <
class TypeBase,
typename... TypeInput>
315template <
class TypeBase,
typename... TypeInput>
316inline Factory<TypeBase, TypeInput...>::~Factory()
321template <
class TypeBase,
typename... TypeInput>
324 if (get().callbacks_.count(_type) == 1 and get().callbacks_.at(_type) != createFn)
325 throw std::runtime_error(get().getClass() +
" : For the type \"" + _type +
326 "\": Trying to register a creator but there is a different creator registered.");
328 bool reg = get().callbacks_.insert(
typename CallbackMap::value_type(_type, createFn)).second;
330 if (reg) std::cout << std::setw(31) << std::left << get().getClass() <<
" <-- registered " << _type << std::endl;
335template <
class TypeBase,
typename... TypeInput>
338 return get().callbacks_.erase(_type) == 1;
341template <
class TypeBase,
typename... TypeInput>
344 return get().callbacks_.count(_type) == 1;
347template <
class TypeBase,
typename... TypeInput>
350 typename CallbackMap::const_iterator creator_callback_it = get().callbacks_.find(_type);
352 if (creator_callback_it == get().callbacks_.end())
354 throw std::runtime_error(get().getClass() +
" : Unknown type \"" + _type +
355 "\". Possibly you tried to use an unregistered creator.");
358 return (creator_callback_it->second)(std::forward<TypeInput>(_input)...);
361template <
class TypeBase,
typename... TypeInput>
364 static Factory<TypeBase, TypeInput...> instance_;
368template <
class TypeBase,
typename... TypeInput>
371 return "Factory<class TypeBase>";
374template <
class TypeBase,
typename... TypeInput>
377 std::cout << get().getClass() <<
" address: " << &get() << std::endl;
380template <
class TypeBase,
typename... TypeInput>
383 std::cout << get().getClass() <<
" callbacks size: " << get().callbacks_.size() << std::endl;
384 for (
auto cb : get().callbacks_)
386 std::cout <<
"\t" << cb.first << std::endl;
390template <
class TypeBase,
typename... TypeInput>
393 std::list<std::string> keys;
394 for (
auto cb : get().callbacks_)
396 keys.push_back(cb.first);
Singleton template factory.
Definition: factory.h:238
static void printCallbacks()
Prints the registered types.
Definition: factory.h:381
static bool registerCreator(const std::string &_type, CreatorCallback createFn)
Registers the creator of a type. Throws if a different creator is already registered for it.
Definition: factory.h:322
static TypeBase create(const std::string &_type, TypeInput... _input)
Creates an object of a type with its registered creator. Throws if no creator is registered.
Definition: factory.h:348
static std::list< std::string > getRegisteredKeys()
Returns the registered types.
Definition: factory.h:391
TypeBase(* CreatorCallback)(TypeInput... _input)
Example of creator callback (see typedefs below)
Definition: factory.h:241
static bool isCreatorRegistered(const std::string &_type)
Returns whether a creator is registered for a type.
Definition: factory.h:342
void operator=(Factory const &)=delete
Not assignable (singleton)
std::string getClass() const
Returns the name of the factory.
Definition: factory.h:369
Factory(const Factory &)=delete
Not copyable (singleton)
static void printAddress()
Prints the address of the factory singleton.
Definition: factory.h:375
static bool unregisterCreator(const std::string &_type)
Unregisters the creator of a type. Returns false if it was not registered.
Definition: factory.h:336
Definition: capture_base.h:23